/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.date.DateUtil
 *  cn.hutool.core.util.ObjectUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.beans.value.ChangeListener
 *  javafx.beans.value.ObservableValue
 *  javafx.collections.FXCollections
 *  javafx.collections.ObservableList
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.Node
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.CheckBox
 *  javafx.scene.control.TableCell
 *  javafx.scene.control.TableColumn
 *  javafx.scene.control.TableView
 *  javafx.scene.control.cell.PropertyValueFactory
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.AnchorPane
 *  javafx.stage.Stage
 *  javafx.util.Callback
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.util.CollectionUtils
 */
package com.gys.controller.salesReceipts;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.gys.common.business.SalesReceiptsUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.util.SpringUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.salesReceipts.SalesReceiptsMainController;
import com.gys.data.example.GetUserOutData;
import com.gys.data.salesReceipts.GetRestOrderDetailOutData;
import com.gys.data.salesReceipts.GetRestOrderInfoOutData;
import com.gys.view.salesReceipts.GetRestOrderView;
import de.felixroske.jfxsupport.FXMLController;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

@FXMLController
public class GetRestOrderController
extends BaseController
implements Initializable {
    private static final Logger log = LoggerFactory.getLogger(GetRestOrderController.class);
    @FXML
    private TableView<GetRestOrderInfoOutData> tableViewInfo;
    @FXML
    private TableColumn<GetRestOrderInfoOutData, CheckBox> checkBox;
    @FXML
    private TableColumn<GetRestOrderInfoOutData, String> gsshDate;
    @FXML
    private TableColumn<GetRestOrderInfoOutData, String> gsshTime;
    @FXML
    private TableColumn<GetRestOrderInfoOutData, String> gsshBillNo;
    @FXML
    private TableColumn<GetRestOrderInfoOutData, String> gsshHykNo;
    @FXML
    private TableColumn<GetRestOrderInfoOutData, String> gsshHykName;
    @FXML
    private TableColumn<GetRestOrderInfoOutData, String> gsshYsAmt;
    @FXML
    private TableView<GetRestOrderDetailOutData> tableViewDetail;
    @FXML
    private TableColumn<GetRestOrderDetailOutData, String> gssdSerial;
    @FXML
    private TableColumn<GetRestOrderDetailOutData, String> gssdProId;
    @FXML
    private TableColumn<GetRestOrderDetailOutData, String> gssdProName;
    @FXML
    private TableColumn<GetRestOrderDetailOutData, String> spec;
    @FXML
    private TableColumn<GetRestOrderDetailOutData, String> proFactoryName;
    @FXML
    private TableColumn<GetRestOrderDetailOutData, String> gssdQty;
    @FXML
    private TableColumn<GetRestOrderDetailOutData, String> gssdPrc1;
    @FXML
    private AnchorPane pane;
    @Autowired
    private SalesReceiptsMainController salesReceiptsMainController;
    private ObservableList<GetRestOrderInfoOutData> observableTableList = FXCollections.observableArrayList(new ArrayList());
    private ObservableList<GetRestOrderDetailOutData> observableTableDetailList = FXCollections.observableArrayList(new ArrayList());
    private GetRestOrderInfoOutData selectInfo;

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isEmpty(this.pane)) {
            return;
        }
        this.selectInfo = null;
        this.bangDing();
        this.tableViewDetail.getItems().clear();
        this.tableViewDetail.refresh();
        this.quickKey();
        this.tableClick();
        this.initTable();
        if (this.tableViewInfo.getItems().size() > 0) {
            this.tableViewInfo.requestFocus();
            this.tableViewInfo.getSelectionModel().select(0);
        }
    }

    private void quickKey() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask(){

            @Override
            public void run() {
                GetRestOrderView view = SpringUtil.getBean(GetRestOrderView.class);
                Stage stage = (Stage)view.getView().getScene().getWindow();
                stage.getScene().setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

                    public void handle(KeyEvent e) {
                        if (e.getCode() == KeyCode.UP || e.getCode() == KeyCode.DOWN) {
                            GetRestOrderInfoOutData outData = (GetRestOrderInfoOutData)GetRestOrderController.this.tableViewInfo.getSelectionModel().getSelectedItem();
                            if (GetRestOrderController.this.tableViewInfo.getItems().size() == 0) {
                                return;
                            }
                            if (ObjectUtil.isEmpty(outData)) {
                                GetRestOrderController.this.tableViewInfo.getSelectionModel().select(0);
                                outData = (GetRestOrderInfoOutData)GetRestOrderController.this.tableViewInfo.getItems().get(0);
                            }
                            outData.getCheckBox().setSelectStatus(true);
                            int index = 0;
                            for (int i = 0; i < GetRestOrderController.this.tableViewInfo.getItems().size(); ++i) {
                                GetRestOrderInfoOutData getRestOrderInfoOutData = (GetRestOrderInfoOutData)GetRestOrderController.this.tableViewInfo.getItems().get(i);
                                if (outData != getRestOrderInfoOutData) {
                                    getRestOrderInfoOutData.getCheckBox().setSelectStatus(false);
                                    continue;
                                }
                                index = i;
                            }
                            GetRestOrderController.this.handleSelect(index);
                        } else if (e.getCode() == KeyCode.ENTER) {
                            GetRestOrderController.this.sureAction();
                        } else if (e.getCode() == KeyCode.ESCAPE) {
                            GetRestOrderController.this.cancelAction();
                        }
                    }
                });
            }
        }, 500L);
    }

    private void bangDing() {
        this.checkBox.setCellValueFactory(cellData -> ((GetRestOrderInfoOutData)cellData.getValue()).getCheckBox().getCustomCheckBox());
        this.gsshDate.setCellValueFactory((Callback)new PropertyValueFactory(this.gsshDate.getId()));
        this.gsshTime.setCellValueFactory((Callback)new PropertyValueFactory(this.gsshTime.getId()));
        this.gsshBillNo.setCellValueFactory((Callback)new PropertyValueFactory(this.gsshBillNo.getId()));
        this.gsshHykNo.setCellValueFactory((Callback)new PropertyValueFactory(this.gsshHykNo.getId()));
        this.gsshHykName.setCellValueFactory((Callback)new PropertyValueFactory(this.gsshHykName.getId()));
        this.gsshYsAmt.setCellValueFactory((Callback)new PropertyValueFactory(this.gsshYsAmt.getId()));
        this.gssdSerial.setCellValueFactory((Callback)new PropertyValueFactory(this.gssdSerial.getId()));
        this.gssdProId.setCellValueFactory((Callback)new PropertyValueFactory(this.gssdProId.getId()));
        this.gssdProName.setCellValueFactory((Callback)new PropertyValueFactory(this.gssdProName.getId()));
        this.spec.setCellValueFactory((Callback)new PropertyValueFactory(this.spec.getId()));
        this.proFactoryName.setCellValueFactory((Callback)new PropertyValueFactory(this.proFactoryName.getId()));
        this.gssdQty.setCellValueFactory((Callback)new PropertyValueFactory(this.gssdQty.getId()));
        this.gssdPrc1.setCellValueFactory((Callback)new PropertyValueFactory(this.gssdPrc1.getId()));
    }

    private void initTable() {
        this.tableViewInfo.getItems().clear();
        this.tableViewInfo.refresh();
        List outDataList = null;
        HashMap<String, Object> param = new HashMap<String, Object>();
        param.put("gsshHideFlag", "1");
        param.put("gsshDate", DateUtil.format((Date)DateUtil.date(), (String)"yyyyMMdd"));
        param.put("gsshCallAllow", "1");
        HttpJson httpJson = SalesReceiptsUtil.getRestOrder(param);
        if (!httpJson.isSuccess()) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", httpJson.getMsg());
            alert.showAndWait();
            return;
        }
        outDataList = JSON.parseArray((String)httpJson.getMsg(), GetRestOrderInfoOutData.class);
        this.observableTableList = FXCollections.observableArrayList((Collection)outDataList);
        this.tableViewInfo.setItems(this.observableTableList);
        this.tableViewInfo.refresh();
        this.checkBox.setCellFactory(col -> {
            TableCell<GetRestOrderInfoOutData, CheckBox> cell = new TableCell<GetRestOrderInfoOutData, CheckBox>(){

                protected void updateItem(CheckBox item, boolean empty) {
                    super.updateItem(item, empty);
                    this.setGraphic((Node)item);
                    if (item != null) {
                        item.setOnMouseClicked(col -> {
                            int index = this.getIndex();
                            GetRestOrderController.this.handleSelect(index);
                        });
                    }
                }
            };
            return cell;
        });
    }

    private void tableClick() {
        this.tableViewInfo.getSelectionModel().selectedItemProperty().addListener((ChangeListener)new ChangeListener<GetRestOrderInfoOutData>(){

            public void changed(ObservableValue<? extends GetRestOrderInfoOutData> observableValue, GetRestOrderInfoOutData oldItem, GetRestOrderInfoOutData newItem) {
                if (ObjectUtil.isEmpty(newItem)) {
                    return;
                }
                int index = 0;
                for (int i = 0; i < GetRestOrderController.this.observableTableList.size(); ++i) {
                    if (!newItem.getGsshBillNo().equals(((GetRestOrderInfoOutData)GetRestOrderController.this.observableTableList.get(i)).getGsshBillNo())) continue;
                    index = i;
                    break;
                }
                GetRestOrderController.this.handleSelect(index);
            }
        });
    }

    private void handleSelect(int index) {
        if (CollectionUtils.isEmpty(this.observableTableList)) {
            return;
        }
        for (int i = 0; i < this.observableTableList.size(); ++i) {
            if (i != index) {
                ((GetRestOrderInfoOutData)this.observableTableList.get(i)).getCheckBox().setSelectStatus(false);
                continue;
            }
            ((GetRestOrderInfoOutData)this.observableTableList.get(i)).getCheckBox().setSelectStatus(true);
        }
        if (!((GetRestOrderInfoOutData)this.observableTableList.get(index)).getCheckBox().isSelected().booleanValue()) {
            this.selectInfo = null;
            this.tableViewDetail.getItems().clear();
            this.tableViewDetail.refresh();
            return;
        }
        this.selectInfo = (GetRestOrderInfoOutData)this.observableTableList.get(index);
        this.initDetailTableView((GetRestOrderInfoOutData)this.observableTableList.get(index));
    }

    private void initDetailTableView(GetRestOrderInfoOutData infoOutData) {
        this.tableViewDetail.getItems().clear();
        this.tableViewDetail.refresh();
        List outDataList = null;
        HashMap<String, Object> param = new HashMap<String, Object>();
        GetUserOutData loginUser = this.getLoginUser();
        param.put("clientId", loginUser.getClient());
        param.put("gssdBillNo", infoOutData.getGsshBillNo());
        param.put("gssdBrId", loginUser.getDepId());
        param.put("gssdDate", DateUtil.format((Date)DateUtil.date(), (String)"yyyyMMdd"));
        HttpJson httpJson = SalesReceiptsUtil.getRestOrderDetail(param);
        if (!httpJson.isSuccess()) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", httpJson.getMsg());
            alert.showAndWait();
            return;
        }
        outDataList = JSON.parseArray((String)httpJson.getMsg(), GetRestOrderDetailOutData.class);
        this.observableTableDetailList = FXCollections.observableArrayList((Collection)outDataList);
        this.tableViewDetail.setItems(this.observableTableDetailList);
    }

    @FXML
    private void sureAction() {
        if (ObjectUtil.isEmpty(this.selectInfo)) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8bf7\u9009\u62e9\u8bb0\u5f55");
            alert.showAndWait();
            return;
        }
        this.salesReceiptsMainController.restOrderInfo(this.selectInfo, (List<GetRestOrderDetailOutData>)this.tableViewDetail.getItems());
        this.cancelAction();
    }

    @FXML
    private void cancelAction() {
        this.pane.getScene().getWindow().hide();
    }
}

